package com.ruoyi.socket.util.midr;



import com.ruoyi.socket.util.midr.hl7.Message;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/***
 *  @description
 *  @author zhao
 *  @date: 2020/6/16
 *  @version: v1.0
 */


public class MariRun {
    public static void main(String[] args) throws InterruptedException {
        Mindrayparse m = new Mindrayparse(4603);
        m.start();
    }
}

class Mindrayparse extends Thread {

    String read;
    int PORT;
    boolean first = true;
    ServerSocket welcomeSocket = null;
    Socket connSock = null;
    BufferedReader inFromEquipment = null;
    boolean stopped = false;

    public Mindrayparse(int PORT) {
        this.PORT = PORT;
    }

    @Override
    public void run() {
        try {
            welcomeSocket = new ServerSocket(PORT);
            connSock = welcomeSocket.accept();
            first = false;
            System.out.println("Client has started");
            String input = "";
            while (true) {
                try {
                    inFromEquipment = new BufferedReader(new InputStreamReader(connSock.getInputStream()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                read = "";
                while (true) {
                    try {
                        if (!((input = inFromEquipment.readLine()).length() > 1))
                            break;

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    read = read + input + "<::>";
                }
                if (!read.isEmpty()) {
                    String outJson = handleMessage(read);
                    System.out.println("----解析结果-----");
                    System.out.println(outJson);
                }
            }
        } catch (IOException e) {
            System.out.println("端口被占用");
            e.printStackTrace();
        }

    }

    public String handleMessage(String message) {

//        String filepath="\"/Users/zhiheng/Desktop/Montiorparsedevice/Montiorsdk/src/main/java/HL7/MindrayN1Interface.xml\"";

        File file = new File("src/main/resources/midr/MindrayN1Interface.xml");
//        File file = new File("/Users/zhiheng/Desktop/Montiorparsedevice/Montiorsdk/src/main/java/HL7/MindrayN1Interface.xml");
        Parsehl7 p = new Parsehl7();

        try {
            System.out.println("从机器收到数据: " + message);
            Message msg = p.getMindrayMessage(message, file);
            System.out.println("类型______________________________________________________88888888888888888888888888888888888888：" + msg.type);
            String outJson = p.MessagedJson(msg);
            System.out.println("解析结果JSON——————————————: " + outJson);
        } catch (Exception ex) {

        }
        return null;
    }

}



